package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.device.csun.camera.entity.CsunCameraData;
import com.csun.cmny.provider.device.csun.gateway.entity.CsunGatewayData;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.constant.DeviceConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.csunsleep.CsunSleepWifiDto;
import com.csun.cmny.provider.model.dto.device.DevicePageQuery;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.gateway.*;
import com.csun.cmny.provider.model.enums.device.DeviceStatusEnum;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.RoomBuildingVo;
import com.csun.cmny.provider.model.vo.device.DmcDeviceVo;
import com.csun.cmny.provider.model.vo.gateway.CsunGatewayWifiVO;
import com.csun.cmny.provider.model.vo.gateway.DmcGatewayVo;
import com.csun.cmny.provider.model.vo.gateway.GatewayBindVo;
import com.csun.cmny.provider.model.vo.humidistat.CsunHumidistatVo;
import com.csun.cmny.provider.model.vo.newBody.CsunNewBodyVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 13:15
 * @Description: the class dmc device csun gateway controller.
 */
@RestController
@RequestMapping(value = "/gateway", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcCsunGatewayController extends BaseController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcGatewayDataService dmcGatewayDataService;

    @Resource
    private DmcGatewayDataHiService dmcGatewayDataHiService;

    @Resource
    private DmcCsunGatewayBindService dmcCsunGatewayBindService;

    @Resource
    private DmcDeviceOnlineRecordService dmcDeviceOnlineRecordService;

    @Resource
    private DmcCsunGatewayLocationService dmcCsunGatewayLocationService;

    @Resource
    private DmcCsunHumidistatLocationService dmcCsunHumidistatLocationService;

    @Resource
    private DmcCsunNewBodyLocationService dmcCsunNewBodyLocationService;

    @Resource
    private DmcCsunGatewayWifiService dmcCsunGatewayWifiService;

    /**
     * 查询组织智能网关设备列表
     *
     * @param csunGatewayQueryDto the device page query
     * @return the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "查询组织智能网关设备列表")
    @RequestMapping(value = "/queryGroupCsunGatewayListWithPage", method = RequestMethod.POST)
    public Wrapper queryGroupCsunGatewayListWithPage(@RequestBody CsunGatewayQueryDto csunGatewayQueryDto) {

        csunGatewayQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        csunGatewayQueryDto.setDeviceType(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex());

        PageInfo pageInfo = dmcCsunGatewayLocationService.queryDeviceListWithPage(csunGatewayQueryDto);

        OldmanBedRoomBuildingVo obv, bov;
        RoomBuildingVo rbv;
        for (Object o : pageInfo.getList()) {
            DmcGatewayVo dmcGatewayVo = (DmcGatewayVo) o;
            //查询网关在线情况
            CsunGatewayData csunGatewayData = (CsunGatewayData) redisTemplate.opsForValue()
                    .get(RedisKeyUtil.getCsunGatewayKey(dmcGatewayVo.getDeviceNo()));

            if (PublicUtil.isNotEmpty(csunGatewayData)) {
                dmcGatewayVo.setStatus(csunGatewayData.getStatus());
            } else {
                dmcGatewayVo.setStatus(DeviceStatusEnum.OFFLINE.getKey());
            }
            //获取实时位置
            if (dmcGatewayVo.isBind()) {
                switch (dmcGatewayVo.getBindType()) {
                    case DeviceConstant.DEVICE_BIND_TYPE_OLDMAN:
                        obv = (OldmanBedRoomBuildingVo) redisTemplate
                                .opsForValue().get(RedisKeyUtil.getOmcOldmanId(dmcGatewayVo.getOldmanId()));
                        if (PublicUtil.isNotEmpty(obv)) {
                            dmcGatewayVo.setOldmanName(obv.getOldmanName());
                            if (PublicUtil.isNotEmpty(obv.getRoomNo()) && PublicUtil.isNotEmpty(obv.getBedNo())) {
                                dmcGatewayVo.setLocation(obv.getRoomNo() + "-" + obv.getBedNo());
                            } else {
                                dmcGatewayVo.setLocation("--");
                            }
                        }
                        break;
                    case DeviceConstant.DEVICE_BIND_TYPE_BED:
                        bov = (OldmanBedRoomBuildingVo) redisTemplate
                                .opsForValue().get(RedisKeyUtil.getOmcBedId(dmcGatewayVo.getBedId()));
                        if (PublicUtil.isNotEmpty(bov)) {
                            dmcGatewayVo.setOldmanName(bov.getOldmanName());
                            if (PublicUtil.isNotEmpty(bov.getRoomNo()) && PublicUtil.isNotEmpty(bov.getBedNo())) {
                                dmcGatewayVo.setLocation(bov.getRoomNo() + "-" + bov.getBedNo());
                            } else {
                                dmcGatewayVo.setLocation("--");
                            }
                            dmcGatewayVo.setOldmanId(bov.getOldmanId());
                        }
                        break;
                    case DeviceConstant.DEVICE_BIND_TYPE_ROOM:
                        rbv = (RoomBuildingVo) redisTemplate
                                .opsForValue().get(RedisKeyUtil.getOmcRoomId(dmcGatewayVo.getRoomId()));
                        if (PublicUtil.isNotEmpty(rbv)) {
                            dmcGatewayVo.setLocation(rbv.getRoomNo());
                        } else {
                            dmcGatewayVo.setLocation("--");
                        }
                        break;
                    case DeviceConstant.DEVICE_BIND_TYPE_LIFE:
                        rbv = (RoomBuildingVo) redisTemplate
                                .opsForValue().get(RedisKeyUtil.getOmcRoomId(dmcGatewayVo.getRoomId()));
                        if (PublicUtil.isNotEmpty(rbv)) {
                            dmcGatewayVo.setLocation(rbv.getRoomNo());
                            dmcGatewayVo.setBuildingName(rbv.getBuildingName());
                            dmcGatewayVo.setFloorNo(rbv.getFloorNo());
                            dmcGatewayVo.setRoomNo(rbv.getRoomNo());
                            dmcGatewayVo.setOldmanId(rbv.getOldmanId());
                            dmcGatewayVo.setOldmanName(rbv.getOldmanName());
                        } else {
                            dmcGatewayVo.setLocation("--");
                        }
                        break;
                }
            }
            if (dmcGatewayVo.getLocation() == null) {
                dmcGatewayVo.setLocation("");
            }
        }

        // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    /**
     * 添加网关设备
     *
     * @param dmcEditDeviceDto the dmc edit device dto
     * @param br               the binding result
     * @return the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "添加网关设备")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addCsunGateway(@Validated @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex());

        Integer result = dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            CsunGatewayData csunGatewayData = new CsunGatewayData();
            csunGatewayData.setDeviceId(dmcDevice.getId());
            csunGatewayData.setDeviceNo(dmcDevice.getDeviceNo());

            redisTemplate.opsForValue().set(RedisKeyUtil.getCsunGatewayKey(dmcDevice.getDeviceNo()),
                    csunGatewayData);
        }

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 修改网关设备
     *
     * @param dmcEditDeviceDto the dmc edit device dto
     * @param br               the binding result
     * @return the wrapper
     */
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改网关设备")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper editCsunGateway(@Validated(value = {DmcDeviceEditValidator.class})
                                   @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();
        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);
        dmcDevice.setDeviceType(DmcDeviceTypeEnum.CSUN_GATEWAY.getIndex());

        DmcDevice dmcDeviceOld = dmcDeviceService.selectByKey(dmcEditDeviceDto.getId());

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            // 判断是否有修改deviceNo
            if (!dmcDeviceOld.getDeviceNo().equals(dmcEditDeviceDto.getDeviceNo())) {
                // 删除旧的redisKey(deviceNo)
                String redisKey = RedisKeyUtil.getCsunGatewayKey(dmcDeviceOld.getDeviceNo());
                CsunGatewayData csunGatewayData = (CsunGatewayData) redisTemplate.opsForValue().get(redisKey);
                if (PublicUtil.isNotEmpty(csunGatewayData)) {
                    // 删除旧的
                    redisTemplate.delete(redisKey);
                    // 添加新的
                    csunGatewayData.setDeviceNo(dmcEditDeviceDto.getDeviceNo());
                    csunGatewayData.setStatus(DeviceStatusEnum.OFFLINE.getKey());   // 设备下线
                    redisKey = RedisKeyUtil.getCsunGatewayKey(dmcEditDeviceDto.getDeviceNo());
                    redisTemplate.opsForValue().set(redisKey, csunGatewayData);
                }
            }
        }

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 根据Id删除网关
     *
     * @param id the id
     * @return the wrapper
     */
    @ApiOperation(httpMethod = "POST", value = "根据Id删除网关")
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteDmcCsunGatewayById(@PathVariable Long id) {

        DmcDevice dmcDeviceOld = dmcDeviceService.selectByKey(id);

        /**
         * @date 2020-01-02
         * @author chisj
         * @desc 判断网关是否有绑定其他传感器
         */
        DmcCsunGatewayBind dmcCsunGatewayBind = new DmcCsunGatewayBind();
        dmcCsunGatewayBind.setGatewayId(id);
        int count = dmcCsunGatewayBindService.selectCount(dmcCsunGatewayBind);
        if (count > 0) {
            throw new DmcBizException(ErrorCodeEnum.DMC100614005);
        }

        Integer result = dmcDeviceService.deleteDmcDeviceById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        } else {
            // 删除redisKey(deviceNo)
            redisTemplate.delete(RedisKeyUtil.getCsunGatewayKey(dmcDeviceOld.getDeviceNo()));
            DmcDeviceOnlineRecord dmcDeviceOnlineRecord = new DmcDeviceOnlineRecord();
            dmcDeviceOnlineRecord.setDeviceId(id);
            int delete = dmcDeviceOnlineRecordService.delete(dmcDeviceOnlineRecord);

            // 删除数据
            dmcGatewayDataService.deleteByKey(id);

            DmcGatewayDataHi dmcGatewayDataHi = new DmcGatewayDataHi();
            dmcGatewayDataHi.setDeviceId(id);
            dmcGatewayDataHiService.delete(dmcGatewayDataHi);
        }


        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * query real gateway data by device id
     *
     * @param deviceId device id
     * @return the wrapper
     */
    @RequestMapping(value = "/queryRealGatewayDataByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper queryRealGatewayDataByDeviceId(@PathVariable Long deviceId) {



        DmcGatewayData dmcGatewayData = dmcGatewayDataService.queryRealGatewayDataByDeviceId(deviceId);


        if(PublicUtil.isNotEmpty(dmcGatewayData)){
            Boolean hasKey = redisTemplate.hasKey(RedisKeyUtil.getCsunGatewayOnline(dmcGatewayData.getNetgateNo()));
            if(hasKey){
                dmcGatewayData.setStatus(DeviceStatusEnum.ONLINE.getKey());

            }else {

                dmcGatewayData.setStatus(DeviceStatusEnum.OFFLINE.getKey());
            }
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null));
    }

    /**
     * query real gateway data by device no
     *
     * @param deviceNo device no
     * @return the wrapper
     */
    @RequestMapping(value = "/queryRealGatewayDataByDeviceNo/{deviceNo}", method = RequestMethod.POST)
    public Wrapper queryRealGatewayDataByDeviceNo(@PathVariable String deviceNo) {

        String redisKey = RedisKeyUtil.getCsunGatewayKey(deviceNo);
        CsunGatewayData csunGatewayData = (CsunGatewayData) redisTemplate.opsForValue().get(redisKey);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), csunGatewayData);
    }

    /**
     * query dmc gateway data hi list with page
     *
     * @param gatewayDataHiPageQuery the gateway data hi query page
     * @return the wrapper
     */
    @RequestMapping(value = "/queryDmcGatewayDataHiListWithPage", method = RequestMethod.POST)
    public Wrapper queryDmcGatewayDataHiListWithPage(@RequestBody GatewayDataHiPageQuery gatewayDataHiPageQuery) {

        PageInfo pageInfo = dmcGatewayDataHiService.queryDmcGatewayDataHiListWithPage(gatewayDataHiPageQuery);

        // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    /**
     * 查看网关绑定温湿度情况
     */
    @RequestMapping(value = "/queryGatewayBindHumidistatList", method = RequestMethod.POST)
    public Wrapper queryGatewayBindHumidistatList(@RequestBody GatewayBindDto gatewayBindDto) {

        Preconditions.checkArgument(PublicUtil.isNotEmpty(gatewayBindDto.getGatewayId()),
                cmnyMessageSource.getMessage("gateway.id.null", null));

        gatewayBindDto.setGroupId(getLoginAuthDto().getGroupId());
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.HUMIDISTAT.getIndex());

        List<GatewayBindVo> gatewayBindVoList = dmcCsunGatewayBindService.queryGatewayBindList(gatewayBindDto);

        // return WrapMapper.ok(gatewayBindVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), gatewayBindVoList);
    }

    /**
     * 查看网关绑定新人感情况
     */
    @RequestMapping(value = "/queryGatewayBindNewBodySensorList", method = RequestMethod.POST)
    public Wrapper queryGatewayBindNewBodySensorList(@RequestBody GatewayBindDto gatewayBindDto) {

        Preconditions.checkArgument(PublicUtil.isNotEmpty(gatewayBindDto.getGatewayId()),
                cmnyMessageSource.getMessage("gateway.id.null", null));

        gatewayBindDto.setGroupId(getLoginAuthDto().getGroupId());
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.NEWBODY.getIndex());

        List<GatewayBindVo> gatewayBindVoList = dmcCsunGatewayBindService.queryGatewayBindList(gatewayBindDto);

        // return WrapMapper.ok(gatewayBindVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), gatewayBindVoList);
    }

    /**
     * 查看网关绑定温湿度情况
     */
    @RequestMapping(value = "/queryGatewayBindButtonList", method = RequestMethod.POST)
    public Wrapper queryGatewayBindButtonList(@RequestBody GatewayBindDto gatewayBindDto) {

        Preconditions.checkArgument(PublicUtil.isNotEmpty(gatewayBindDto.getGatewayId()),
                cmnyMessageSource.getMessage("gateway.id.null", null));

        gatewayBindDto.setGroupId(getLoginAuthDto().getGroupId());
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.CSUN_BUTTON.getIndex());

        List<GatewayBindVo> gatewayBindVoList = dmcCsunGatewayBindService.queryGatewayBindList(gatewayBindDto);

        // return WrapMapper.ok(gatewayBindVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), gatewayBindVoList);
    }

    /**
     * 查看网关绑定温湿度情况
     */
    @RequestMapping(value = "/queryGatewayBindBodySensorList", method = RequestMethod.POST)
    public Wrapper queryGatewayBindBodySensorList(@RequestBody GatewayBindDto gatewayBindDto) {

        Preconditions.checkArgument(PublicUtil.isNotEmpty(gatewayBindDto.getGatewayId()),
                cmnyMessageSource.getMessage("gateway.id.null", null));

        gatewayBindDto.setGroupId(getLoginAuthDto().getGroupId());
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.CSUN_BODY_SENSOR.getIndex());

        List<GatewayBindVo> gatewayBindVoList = dmcCsunGatewayBindService.queryGatewayBindList(gatewayBindDto);

        // return WrapMapper.ok(gatewayBindVoList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), gatewayBindVoList);
    }

    /**
     * 网关绑定温湿度器
     */
    @RequestMapping(value = "/gatewayBindHumidistatList", method = RequestMethod.POST)
    public Wrapper gatewayBindHumidistatList(@RequestBody GatewayBindDto gatewayBindDto) {

        gatewayBindDto.setGroupId(getLoginAuthDto().getGroupId());
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.HUMIDISTAT.getIndex());

        Integer result = dmcCsunGatewayBindService.gatewayBind(gatewayBindDto);

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 网关绑定 - 新人感器
     */
    @RequestMapping(value = "/gatewayBindNewBodySensorList", method = RequestMethod.POST)
    public Wrapper gatewayBindNewBodySensorList(@RequestBody GatewayBindDto gatewayBindDto) {

        gatewayBindDto.setGroupId(getLoginAuthDto().getGroupId());
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.NEWBODY.getIndex());

        Integer result = dmcCsunGatewayBindService.gatewayBind(gatewayBindDto);

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }


    /**
     * 网关绑定 - 老人感器
     */
    @RequestMapping(value = "/gatewayBindBodySensorList", method = RequestMethod.POST)
    public Wrapper gatewayBindBodySensorList(@RequestBody GatewayBindDto gatewayBindDto) {

        gatewayBindDto.setGroupId(getLoginAuthDto().getGroupId());
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.CSUN_BODY_SENSOR.getIndex());

        Integer result = dmcCsunGatewayBindService.gatewayBind(gatewayBindDto);

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 网关绑定按钮
     */
    @RequestMapping(value = "/gatewayBindButtonList", method = RequestMethod.POST)
    public Wrapper gatewayBindButtonList(@RequestBody GatewayBindDto gatewayBindDto) {

        gatewayBindDto.setGroupId(getLoginAuthDto().getGroupId());
        gatewayBindDto.setBindType(DmcDeviceTypeEnum.CSUN_BUTTON.getIndex());

        Integer result = dmcCsunGatewayBindService.gatewayBind(gatewayBindDto);

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 网关绑定房间
     */
    @RequestMapping(value = "/bind", method = RequestMethod.POST)
    public Wrapper bind(@Validated @RequestBody BindCsunGatewayLocationDto bindCsunGatewayLocationDto, BindingResult br) {

        Integer result = dmcCsunGatewayLocationService.bind(bindCsunGatewayLocationDto, getLoginAuthDto());

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 解绑网关
     *
     * @param deviceId the device id
     * @return the wrapper.
     */
    @RequestMapping(value = "/untied/{deviceId}", method = RequestMethod.POST)
    public Wrapper untied(@PathVariable Long deviceId) {

        Integer result = dmcCsunGatewayLocationService.untied(deviceId);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        DmcDeviceOnlineRecord dmcDeviceOnlineRecord = new DmcDeviceOnlineRecord();
        dmcDeviceOnlineRecord.setDeviceId(deviceId);
        int delete = dmcDeviceOnlineRecordService.delete(dmcDeviceOnlineRecord);
        // return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 查询网关绑定的温度计数据
     *
     * @param deviceId the device id
     * @return the wrapper.
     */
    @RequestMapping(value = "/listGatewayBindHumidistat/{deviceId}", method = RequestMethod.POST)
    public Wrapper listGatewayBindHumidistat(@PathVariable Long deviceId) {

        List<CsunHumidistatVo> csunHumidistatVoList = dmcCsunHumidistatLocationService.listGatewayBindHumidistat(deviceId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), csunHumidistatVoList);
    }

    /**
     * 查询网关绑定的人感数据
     *
     * @param deviceId the device id
     * @return the wrapper.
     */
    @RequestMapping(value = "/listGatewayBindBodySensor/{deviceId}", method = RequestMethod.POST)
    public Wrapper listGatewayBindBodySensor(@PathVariable Long deviceId) {

        List<CsunNewBodyVo> voList = dmcCsunNewBodyLocationService.listGatewayBindBodySensor(deviceId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), voList);
    }

    /**
     * 设置网关wifi
     * @param: [csunGatewayWifiDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/setCsunGatewayWifi", method = RequestMethod.POST)
    public Wrapper setCsunGatewayWifi(@RequestBody CsunGatewayWifiDto csunGatewayWifiDto) {
        int ret = dmcCsunGatewayWifiService.setCsunGatewayWifi(csunGatewayWifiDto,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),ret);
    }

    /**
     * 获取网关wifi设置
     * @param: [deviceId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/getCsunGatewayWifi/{deviceId}", method = RequestMethod.POST)
    public Wrapper getCsunSleepWifi(@PathVariable Long deviceId) {
        DmcCsunGatewayWifi dmcCsunGatewayWifi = dmcCsunGatewayWifiService.selectByKey(deviceId);
        CsunGatewayWifiVO vo = new CsunGatewayWifiVO();
        if(dmcCsunGatewayWifi != null){
            BeanUtils.copyProperties(dmcCsunGatewayWifi,vo);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),vo);
    }


    @RequestMapping(value = "/selectGateWayOnline/{deviceId}", method = RequestMethod.POST)
    public Wrapper selectGateWayOnline(@PathVariable Long deviceId) {

        DmcDevice dmcDevice = dmcDeviceService.selectByKey(deviceId);

        String csunGatewayKey = RedisKeyUtil.getCsunGatewayKey(dmcDevice.getDeviceNo());
        CsunGatewayData csunGatewayData = (CsunGatewayData) redisTemplate.opsForValue().get(csunGatewayKey);


        if (PublicUtil.isNotEmpty(csunGatewayData)){
            if (csunGatewayData.getStatus().equals(DeviceStatusEnum.ONLINE.getKey())){
                return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),DeviceStatusEnum.ONLINE.getKey());
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null),DeviceStatusEnum.OFFLINE.getKey());


    }
}
